package com.lql.lqlreader.login.ui

import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.lql.lqlreader.BaseActivity
import com.lql.lqlreader.R
import com.lql.lqlreader.login.model.User
import com.lql.lqlreader.login.model.postRegister
import com.lql.lqlreader.service.subscriber.HttpResultSubscriber
import kotlinx.android.synthetic.main.activity_register.*
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import timber.log.Timber

class RegisterActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        setLeftTitleAndDisplayHomeAsUp("注册")

        btn_goto_reg.setOnClickListener {
            if (TextUtils.isEmpty(et_username.text)) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (TextUtils.isEmpty(et_password.text)) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            postRegister(et_username.text.toString(), et_password.text.toString())
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : HttpResultSubscriber<User>() {
                        override fun onSuccess(data: User?) {
                            Timber.e(data.toString())
                            Toast.makeText(this@RegisterActivity, "注册成功！", Toast.LENGTH_SHORT).show()
                            finish()
                        }

                        override fun _onError(status: Int, msg: String?) {
                            Toast.makeText(this@RegisterActivity, msg, Toast.LENGTH_SHORT).show()
                        }
                    })
        }
    }
}
